package com.zl.rbac.config;

import com.zl.rbac.web.interceptor.CheckLoginInterceptor;
import com.zl.rbac.web.interceptor.CheckPermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置类
 *
 * @author THINK
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Autowired
    private CheckLoginInterceptor checkLoginInterceptor;
    @Autowired
    private CheckPermissionInterceptor checkPermissionInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //注册拦截器
        registry.addInterceptor(checkLoginInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/login.html", "/login", "/static/**");  // 对那些url不进行拦截
        // /login.html   /login   /static/**
        // /static/js/bootstrap/css/bootstrap.css

        // 注意: 所有静态资源(css/js) 的url前面添加/static/ , chrome浏览器已经好了, 火狐浏览器不行
        //  对应火狐浏览器,需要进行资源的映射
        //配置权限访问控制的拦截器
        registry.addInterceptor(checkPermissionInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/login.html", "/login", "/static/**");

    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //进行资源映射,  把url与本地路径对应
        // 把url中包含/static/xxx  对应本地路径是, 在当前类路径下static目录
        //  url: /static/js/bootstrap/css/bootstrap.css   --> classpath:/static/js/bootstrap/css/bootstrap.css
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}